<template>
  {{ formData }}

  <el-row :gutter="20" class="row">
    <el-form :model="formData" label-width="auto" style="max-width: 600px">
      <VueDraggable
        class="item"
        v-model="formItems"
        :animation="150"
        group="formDraggable"
        ghostClass="ghost"
        @update="onUpdate"
        @add="onAdd"
        @remove="remove"
      >
        <div v-for="item in formItems" :key="item.id" class="itemChild">
          <CusFormItem :item="item" v-model="formData[item.id]" />
        </div>
      </VueDraggable>
      <el-form-item label=" ">
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </el-row>
  <!-- {{ formItems }} -->
</template>

<script lang="ts" setup>
import { ref, watchEffect } from 'vue'
import CusFormItem from '../CusFormItem.vue'
import { VueDraggable } from 'vue-draggable-plus'
import type { IFormItem } from '@/types/FormType'

// do not use same name with ref
const formData = ref<{ [key: string]: string | number | boolean | Date | object }>({})

const formItems = ref<IFormItem[]>([
  {
    id: 'select_1741326121234',
    type: 'select',
    label: '城市',
    modelValue: 'city',
    placeholder: '请选择你的城市',
    options: [
      {
        label: '上海',
        value: 'shanghai',
      },
      {
        label: '北京',
        value: 'beijing',
      },
    ],
  },
])

watchEffect(() => {
  console.log(formItems.value)
})

const onSubmit = () => {
  console.log('submit!')
}

function onUpdate() {
  console.log('update')
}

function onAdd() {
  // formItems.value.forEach((item) => {
  //   if (!formData.value[item.id]) {
  //     formData.value[item.id] = ''
  //   }
  // })
}

function remove() {
  console.log('remove')
}
</script>

<style scoped>
.item {
  height: 500px;
}
</style>
